<?php

namespace common\widgets;

use common\assets\jQueryGanttAsset;
use common\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use yii\widgets\InputWidget;

/**
 * Class jQueryGantt
 *
 * @package common\widgets
 */
class jQueryGantt extends InputWidget
{

    /**
     * @var array 配置信息
     */
    public $clientSetting = [];

    /**
     * @var array
     */
    public $defaultClientSetting = [
        'source'       => [],
        'navigate'     => 'scroll',
        'itemsPerPage' => 10,
        'dow'          => ['日', '一', '二', '三', '四', '五', '六'],
        'months'       => ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月',],
        'waitText'     => '数据加载中...',
    ];

    public $targetSelector = '';

    /**
     * {@inheritdoc}
     */
    public function init()
    {
        parent::init();
        $this->clientSetting = ArrayHelper::merge($this->defaultClientSetting, $this->clientSetting);
        if ( ! $this->targetSelector)
        {
            $this->targetSelector = $this->id . '-gantt';
        }
    }

    /**
     * {@inheritdoc}
     */
    public function run()
    {
        parent::run();
        jQueryGanttAsset::register($this->getView());

        $this->renderHolder();
        $this->renderJS();
    }

    /**
     * 输出holder部分
     */
    public function renderHolder()
    {
        echo Html::tag('div', '', [
            'id'    => $this->targetSelector,
            'class' => 'gantt',
        ]);
    }

    public function renderJS()
    {
        //var_dump(Json::encode($this->clientSetting));die();
        $this->view->registerJs("jQuery('#{$this->targetSelector}').gantt(" . Json::encode($this->clientSetting) . ");");
//        $this->view->registerJs('
//			$(".gantt").gantt({
//				source: [{
//					name: "Sprint 0",
//					desc: "Analysis",
//					values: [{
//						from: "/Date(1320192000000)/",
//						to: "/Date(1322401600000)/",
//						label: "Requirement Gathering",
//						customClass: "ganttRed"
//					}]
//				},{
//					name: " ",
//					desc: "Scoping",
//					values: [{
//						from: "/Date(1322611200000)/",
//						to: "/Date(1323302400000)/",
//						label: "Scoping",
//						customClass: "ganttRed"
//					}]
//				},{
//					name: "Sprint 1",
//					desc: "Development",
//					values: [{
//						from: "/Date(1323802400000)/",
//						to: "/Date(1325685200000)/",
//						label: "Development",
//						customClass: "ganttGreen"
//					}]
//				},{
//					name: " ",
//					desc: "Showcasing",
//					values: [{
//						from: "/Date(1325685200000)/",
//						to: "/Date(1325695200000)/",
//						label: "Showcasing",
//						customClass: "ganttBlue"
//					}]
//				},{
//					name: "Sprint 2",
//					desc: "Development",
//					values: [{
//						from: "/Date(1326785200000)/",
//						to: "/Date(1325785200000)/",
//						label: "Development",
//						customClass: "ganttGreen"
//					}]
//				},{
//					name: " ",
//					desc: "Showcasing",
//					values: [{
//						from: "/Date(1328785200000)/",
//						to: "/Date(1328905200000)/",
//						label: "Showcasing",
//						customClass: "ganttBlue"
//					}]
//				},{
//					name: "Release Stage",
//					desc: "Training",
//					values: [{
//						from: "/Date(1330011200000)/",
//						to: "/Date(1336611200000)/",
//						label: "Training",
//						customClass: "ganttOrange"
//					}]
//				},{
//					name: " ",
//					desc: "Deployment",
//					values: [{
//						from: "/Date(1336611200000)/",
//						to: "/Date(1338711200000)/",
//						label: "Deployment",
//						customClass: "ganttOrange"
//					}]
//				},{
//					name: " ",
//					desc: "Warranty Period",
//					values: [{
//						from: "/Date(1336611200000)/",
//						to: "/Date(1349711200000)/",
//						label: "Warranty Period",
//						customClass: "ganttOrange"
//					}]
//				}],
//				navigate: "scroll",
//				maxScale: "hours",
//				itemsPerPage: 10,
//				onItemClick: function(data) {
//					alert("Item clicked - show some details");
//				},
//				onAddClick: function(dt, rowId) {
//					alert("Empty space clicked - add an item!");
//				},
//				onRender: function() {
//					if (window.console && typeof console.log === "function") {
//						console.log("chart rendered");
//					}
//				}
//			});
//        ');
    }
}
